package com.tengju.user.infrastructure.repository.convert;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tengju.data.domain.shared.JsonUtil;
import com.tengju.user.domain.model.sentiment.Sentiment;
import com.tengju.user.domain.model.sentiment.SentimentId;
import com.tengju.user.domain.model.staff.StaffSentimentCount;
import com.tengju.user.domain.model.staff.StaffSentimentRecord;
import com.tengju.user.domain.model.user.SsoUserId;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.FormComponent;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.StaffSentimentCountDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.UserSentimentDO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;


public class UserSentimentConvert {

    public static UserSentimentDO convertToDO(Sentiment sentiment) {
        if (sentiment == null) {
            return null;
        }
        UserSentimentDO userSentimentDO = new UserSentimentDO();
        userSentimentDO.setContext(JsonUtil.toJSONString(sentiment.getFormComponentList()));
        userSentimentDO.setSsoUserId(sentiment.getSsoUserId().getValue());
        userSentimentDO.setOperator(sentiment.getOperator());
        userSentimentDO.setIdCode(sentiment.getIdCode().getValue());
        userSentimentDO.setCreateTime(sentiment.getCreateTime());
        return userSentimentDO;
    }

    public static Sentiment convertToModel(UserSentimentDO userSentimentDO) {
        if (userSentimentDO == null) {
            return null;
        }
        return new Sentiment(JsonUtil.toList(userSentimentDO.getContext(), new TypeReference<>() {}),
                new SsoUserId(userSentimentDO.getSsoUserId()),
                userSentimentDO.getOperator(),
                new UserIdCode(userSentimentDO.getIdCode()),
                userSentimentDO.getCreateTime());
    }

    public static List<StaffSentimentCount> convertToModelCountList(List<StaffSentimentCountDO> sentimentCountDOList) {
        if (CollectionUtils.isEmpty(sentimentCountDOList)) {
            return Collections.emptyList();
        }
        List<StaffSentimentCount> list = new ArrayList<>();
        for (StaffSentimentCountDO staffSentimentCountDO : sentimentCountDOList) {
            list.add(StaffSentimentCount.of(staffSentimentCountDO.getSsoUserId(),
                    staffSentimentCountDO.getDay(),
                    staffSentimentCountDO.getCustomerCount(),
                    staffSentimentCountDO.getSentimentCount()));
        }
        return list;
    }

    public static List<StaffSentimentRecord> convertToModelRecordList(List<UserSentimentDO> userSentimentDOList) {
        if (CollectionUtils.isEmpty(userSentimentDOList)) {
            return Collections.emptyList();
        }
        List<StaffSentimentRecord> list = new ArrayList<>();
        for (UserSentimentDO userSentimentDO : userSentimentDOList) {
            list.add(new StaffSentimentRecord(
                    Optional.ofNullable(userSentimentDO.getSentimentId()).map(SentimentId::new).orElse(null),
                    Optional.ofNullable(userSentimentDO.getSsoUserId()).map(SsoUserId::new).orElse(null),
                    Optional.ofNullable(userSentimentDO.getIdCode()).map(UserIdCode::new).orElse(null),
                    userSentimentDO.getCreateTime(),
                    userSentimentDO.getContext(),
                    userSentimentDO.getOperator()));
        }
        return list;
    }

}
